package com.xjtu.platform.user.assister.consultArrange.impl;

import com.xjtu.platform.user.assister.consultArrange.ConsultArrangeService;
import com.xjtu.platform.user.assister.consultArrange.FillBlankDto;
import com.xjtu.platform.user.assister.consultArrange.FreeTimeDto;
import com.xjtu.platform.user.assister.consultArrange.FreeTimeModel;
import com.xjtu.platform.user.assister.consultArrange.dao.ConsultArrangeDao;
import com.xjtu.platform.common.Constants;
import com.xjtu.platform.common.Utils;
import com.xjtu.platform.common.model.Duty;
import com.xjtu.platform.common.model.User;
import com.xjtu.platform.common.model.Consult;
import com.xjtu.platform.common.model.FirstVisit;
import com.xjtu.platform.common.page.QueryAction;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional
public class ConsultArrangeServiceImpl implements ConsultArrangeService {
    @Resource
    ConsultArrangeDao dao;
    @Override
    public List<FillBlankDto> getAllConsultOfVisit(FirstVisit dto) {
        checkHasConsultToArrange(dto);
        List<FillBlankDto> fillBlankDtos= dao.getAllConsultOfVisit(dto);
        System.out.println("flaggggg1");
        System.out.println(fillBlankDtos.size());
        for (int i = 0; i < fillBlankDtos.size(); i++) {
            System.out.println("flaggggg1");
            System.out.println(fillBlankDtos.size());
            System.out.println(fillBlankDtos.get(i).getTime_id());
            if(fillBlankDtos.get(i).getTime_id()!=null)
            {
                FillBlankDto currentDto = fillBlankDtos.get(i);
                FreeTimeModel model = dao.getTimeInfo(currentDto.getTime_id());
                System.out.println("flaggggg");
                currentDto.setStart_time(model.getStart_time());
                currentDto.setEnd_time(model.getEnd_time());
                fillBlankDtos.set(i, currentDto);
            }
        }
        return fillBlankDtos;
    }

    private void checkHasConsultToArrange(FirstVisit dto) {
        Integer num_consult = dao.checkHasConsultToArrange(dto);
        System.out.println("flag in checkHasConsultToArrange");
        if(num_consult<8)
        {
            for(int i=num_consult;i<8;i++)
            {
                Consult consult = new Consult();
                consult.setConsult_id(UUID.randomUUID().toString());
                consult.setState(1);
                consult.setCount_num(i+1);
                consult.setVisit_id(dto.getVisit_id());
                Duty duty = new Duty();
                duty.setDuty_id(UUID.randomUUID().toString());
                consult.setDuty_id(duty.getDuty_id());
                dao.insertInitDuty(duty);
                dao.insertInitConsult(consult);
            }
        }
    }

    @Override
    public List<User> getAllTeachers() {
        return dao.getAllTeachers();
    }

    @Override
    public Map<String, Object> getFreeTime(FreeTimeDto freeTimeDto) {

        List<FreeTimeModel> list = dao.getFreeTime(freeTimeDto);
        List<String > timeList = new ArrayList<>();
        timeList.add("1");
        timeList.add("2");
        timeList.add("3");
        timeList.add(("4"));
        for (FreeTimeModel freeTimeModel : list) {
            if(timeList.contains(freeTimeModel.getTime_id())){
                timeList.remove(freeTimeModel.getTime_id());
            }
        }
        List<FreeTimeModel> infoList = new ArrayList<>();
        for (String time : timeList) {
            infoList.add(dao.getTimeInfo(time));
        }
        System.out.println(infoList.toString());
        return Utils.getPage(freeTimeDto, new QueryAction<FreeTimeModel>() {
            @Override
            public List<FreeTimeModel> execute() {
                return infoList;
            }
        });
    }

    public String plus7(String day) throws ParseException {
        System.out.println("in plus 7");
        System.out.println(day);
        Calendar calendar = Calendar.getInstance(); //日历时间工具类实例化创建，取得当前时间初值
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date;
        date = sdf.parse(day);
        calendar.setTime(date);  //覆盖掉当前时间
        calendar.add(Calendar.DATE, 7); //+7
        day = sdf.format(calendar.getTime());
        System.out.println(day);
        System.out.println("go out plus 7");
        return day;
    }
    @Override
    public boolean fillBlanks(FillBlankDto dto) {
        System.out.println("flag234");
        System.out.println(dto.getCount_num());
        List<FillBlankDto> listToFill = dao.getConsultNeedToBlank(dto);
        System.out.println(dto.getCount_num());
        String tmp = dto.getDay();
        for (int i = 0; i < listToFill.size(); i++) {
            System.out.println("what???????");
            FillBlankDto currentDto = listToFill.get(i);
            System.out.println(currentDto.getCount_num());
            currentDto.setTime_id(dto.getTime_id());
            try{
                if(i>0)
                {
                    tmp = plus7(tmp);
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            System.out.println("what2???????");
            currentDto.setDay(tmp);
            currentDto.setConsultant_id(dto.getConsultant_id());
            currentDto.setPlace_name(dto.getPlace_name());
            currentDto.setState(Constants.CONSULT_INIT_STATE);
            dao.updateDuty(currentDto);
            //dao.updateConsultDuty(currentDto);
            System.out.println("what3????????????");
            /*
            if(dao.checkDuty(currentDto)==0)
            {
                //currentDto.setDuty_id(UUID.randomUUID().toString());
                dao.updateDuty(currentDto);
                dao.updateConsultDuty(currentDto);
            }
             */
        }
        return false;
    }

    @Override
    public int finishArrange(FirstVisit dto) {
        if(dao.checkNumOfFinishArrange(dto)>=Constants.INIT_NUM_OF_CONSULT)
        {
            Consult consult = new Consult();
            consult.setCount_num(Constants.FIRST_CONSULT_NUM);
            consult.setState(Constants.CONSULTING_STATE);
            consult.setVisit_id(dto.getVisit_id());
            dao.updateConsult(consult);
            dto.setStatus("consulting");
            dao.updateFirstVisit(dto);
            return Constants.CONFIRM_ALL_ARRANGE_SUCCESS;
        }else{
            return Constants.NOT_COMPLETE_ARRANGE;
        }
    }

}
